﻿

#region Usings
using System.IO;
using System.IO.Compression;
#endregion

namespace Nasa8x.Core
{
    /// <summary>
    /// Utility class used for compressing data
    /// using deflate.
    /// </summary>
    public static class Deflate
    {
        #region Static Functions
        /// <summary>
        /// Compresses data
        /// </summary>
        /// <param name="Bytes">The byte array to be compressed</param>
        /// <returns>A byte array of compressed data</returns>
        public static byte[] Compress(byte[] Bytes)
        {
            MemoryStream Stream = new MemoryStream();
            DeflateStream ZipStream = new DeflateStream(Stream, CompressionMode.Compress, true);
            ZipStream.Write(Bytes, 0, Bytes.Length);
            ZipStream.Close();
            return Stream.ToArray();
        }

        /// <summary>
        /// Decompresses data
        /// </summary>
        /// <param name="Bytes">The byte array to be decompressed</param>
        /// <returns>A byte array of uncompressed data</returns>
        public static byte[] Decompress(byte[] Bytes)
        {
            MemoryStream Stream = new MemoryStream();
            DeflateStream ZipStream = new DeflateStream(new MemoryStream(Bytes), CompressionMode.Decompress, true);
            byte[] Buffer = new byte[4096];
            while (true)
            {
                int Size = ZipStream.Read(Buffer, 0, Buffer.Length);
                if (Size > 0) Stream.Write(Buffer, 0, Size);
                else break;
            }
            ZipStream.Close();
            return Stream.ToArray();
        }
        #endregion
    }
}